package com.googlecode.marrowboy.matchers;

import java.util.Arrays;

import org.hamcrest.Description;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;

/**
 * Hamcrest matcher for checking if a {@link String} contains all the specified
 * substrings. It's easier to use this than to call
 * {@link Matchers#containsString(String)} repeatedly.
 * 
 * @author mjg
 */
public class ContainsAllStrings extends TypeSafeMatcher<String> {

	private final String[] strings;

	/**
	 * Convenience constructor.
	 * 
	 * @param strings
	 *            strings to match on
	 */
	public ContainsAllStrings(String... strings) {
		this.strings = strings;
	}

	/**
	 * appends the description of this matcher to the {@link Description}.
	 * 
	 * @param description
	 *            the {@link Description} to append to.
	 */
	public void describeTo(Description description) {
		description.appendText("a string containing all of: " + Arrays.toString(strings));
	}

	/**
	 * Check whether the argument has all the strings.
	 * 
	 * @param item
	 *            the {@link String} to check.
	 * 
	 * @return whether or not it was a match.
	 */
	@Override
	public boolean matchesSafely(String item) {

		if (item == null || strings == null) {
			return false;
		}

		for (String thisString : strings) {

			if (thisString == null) {
				return false;
			}

			if (!item.contains(thisString)) {
				return false;
			}
		}

		return true;

	}

}